package com.lida.jdservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lida.commonuntils.R;
import com.lida.jdservice.entity.AddLoanVo;
import com.lida.jdservice.entity.GetLoanByIdVo;
import com.lida.jdservice.entity.PersonalLoan;
import com.lida.jdservice.entity.PersonalLoanDescription;
import com.lida.jdservice.service.PersonalLoanDescriptionService;
import com.lida.jdservice.service.PersonalLoanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 借贷表 前端控制器
 * </p>
 *
 * @author LiDa
 * @since 2022-02-01
 */
@RestController
@RequestMapping("/jdservice/loan")
@CrossOrigin
public class PersonalLoanController {

    @Autowired
    private PersonalLoanService LoanService;

    @Autowired
    private PersonalLoanDescriptionService Loandescription;

//    @PostMapping("addLoan")
//    public R addLoan(@RequestBody PersonalLoan loan){
//        PersonalLoan.save(loan);
//        return R.success();
//    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable String id){
        LoanService.removeById(id);
        return R.success();
    }

//    @PostMapping("update")
//    public R update(@RequestBody PersonalLoan loan){
//        PersonalLoan.updateById(loan);
//        return R.success();
//    }

    //分页查询全部借贷
    @GetMapping("getLoan/{page}/{limit}")
    public R getLoan(@PathVariable long page,
                     @PathVariable long limit){
        Page<PersonalLoan> list =new Page<>(page,limit);
        QueryWrapper<PersonalLoan> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        LoanService.page(list,wrapper);
        List<PersonalLoan> LoanList=list.getRecords();
        long total =list.getTotal();

        return R.success().data("LoanList",LoanList).data("total",total);
    }

    //分页带条件查询借贷
    @PostMapping("getLoanList/{page}/{limit}")
    public R getLoanList(@PathVariable long page,@PathVariable long limit,
                         @RequestBody(required = false) PersonalLoan personalLoan){
        Page<PersonalLoan> pageList=new Page<>(page,limit);
        QueryWrapper<PersonalLoan> wrapper =new QueryWrapper<>();
        String kind=personalLoan.getKind();
        String target=personalLoan.getTarget();
        String num=personalLoan.getNum();
        Date start=personalLoan.getStartTime();
        Date fianl=personalLoan.getFinalTime();

        if(!StringUtils.isEmpty(kind)){
            wrapper.eq("kind",kind);
        }
        if(!StringUtils.isEmpty(target)){
            wrapper.eq("target",target);
        }
        if(!StringUtils.isEmpty(num)){
            wrapper.eq("num",num);
        }
        if(!StringUtils.isEmpty(start)){
            wrapper.ge("start_time",start);
        }
        if(!StringUtils.isEmpty(fianl)){
            wrapper.lt("final_time",fianl);
        }
        wrapper.orderByDesc("create_time");
        LoanService.page(pageList,wrapper);
        List<PersonalLoan> item=pageList.getRecords();
        long total =pageList.getTotal();

        return R.success().data("item",item).data("total",total);
    }

    @PostMapping("addLoan")
    public R addLoan(@RequestBody(required = false) AddLoanVo loan){
        PersonalLoan personalLoan =new PersonalLoan();
        BeanUtils.copyProperties(loan,personalLoan);
        personalLoan.setStartTime(loan.getStartTime());
        personalLoan.setFinalTime(loan.getFinalTime());
        LoanService.save(personalLoan);

        PersonalLoanDescription description=new PersonalLoanDescription();
        description.setId(personalLoan.getId());
        description.setDescription(loan.getDescription());
        Loandescription.save(description);
        return R.success();
    }

    @GetMapping("getLoanById/{id}")
    public R getLoanById(@PathVariable String id){
        GetLoanByIdVo loanVo=new GetLoanByIdVo();
        PersonalLoan loan=LoanService.getById(id);
        PersonalLoanDescription description=Loandescription.getById(id);

        BeanUtils.copyProperties(loan,loanVo);

        loanVo.setDescription(description.getDescription());

        return R.success().data("loanVo",loanVo);
    }

    @PostMapping("update/{id}")
    public R update(@RequestBody GetLoanByIdVo loan,@PathVariable String id){
        PersonalLoan personalLoan=new PersonalLoan();
        personalLoan.setId(id);
        BeanUtils.copyProperties(loan,personalLoan);
        LoanService.updateById(personalLoan);

        PersonalLoanDescription description=new PersonalLoanDescription();
        BeanUtils.copyProperties(loan,description);
        Loandescription.updateById(description);

        return R.success();
    }
}

